﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Effects;
using System.Windows.Media.Animation;
using System.ComponentModel;

namespace WGM_Client_Net35
{
    /// <summary>
    /// Interaction logic for CreateAccountWindow.xaml
    /// </summary>
    public partial class CreateAccountWindow : Window
    {
        ConnectionHandlerObject _cho = new ConnectionHandlerObject();
        public CreateAccountWindow()
        {
            InitializeComponent();

            DropShadowEffect redGlow = new DropShadowEffect();
            redGlow.Color = Colors.Red;

            _usernameTb.Effect = _passwordTB1.Effect = _passwordTB2.Effect = _secureQTB.Effect = _secureATB.Effect = redGlow;
        }



        private void _createBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (_usernameTb.Text.Trim() == "")
            {
                FlashTextBox(_usernameTb);
                return;
            }
            if (_passwordTB1.Text.Trim() == "" || _passwordTB1.Text != _passwordTB2.Text)
            {
                FlashTextBox(_passwordTB1);
                FlashTextBox(_passwordTB2);
                return;
            }
            if (_secureQTB.Text.Trim() == "")
            {
                FlashTextBox(_secureQTB);
                return;
            }
            if (_secureATB.Text.Trim() == "")
            {
                FlashTextBox(_secureATB);
                return;
            }
            AllControls.IsEnabled = false;
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(bg_DoWork);
            bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_RunWorkerCompleted);
            bg.RunWorkerAsync(new string[]{_usernameTb.Text,_passwordTB1.Text,_secureQTB.Text,_secureATB.Text});
            
            
        }

        void bg_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            AllControls.IsEnabled = true;
            object[] results = e.Result as object[];
            bool success = (bool) results[0];
            if (success)
            {
                if (_cho.IsConnected)
                {
                    MainWindow mainWindow = new MainWindow();
                    mainWindow.Show();
                    mainWindow.SetConnectionHandler(_cho);
                    DialogResult = true;
                }
                else
                {
                    MessageBox.Show("Login error\r\n" + _cho.LastServerMessage, "Wayne's Global Messenger Beta", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show(results[1] as string, "WGM Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            
            
            
            
        }

        void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            string [] info = e.Argument as string[];
            object[] results = new object[4];
            string message = "";
            try
            {
                bool success = _cho.CreateAccount(info[0], info[1], info[2], info[3], out message);
                if (success)
                {
                    _cho.LogOn(info[0], info[1]);
                }
                results[0] = success;
                results[1] = message;
                results[2] = info[0];
                results[3] = info[1];
            }
            catch
            {
                results[0] = false;
                results[1] = "Error contacting server...";
            }
            e.Result = results;
        }

        private void _cancelBtn_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.Close();
        }

        private void _passwordTB1_TextChanged(object sender, TextChangedEventArgs e)
        {
            DropShadowEffect glow = new DropShadowEffect();
            glow.Color = Colors.Red;
            
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim() != "" && _passwordTB1.Text == _passwordTB2.Text)
            {
                glow.Color = Colors.Green;
            }
            _passwordTB1.Effect = _passwordTB2.Effect = glow;
        }

        private void _passwordTB2_TextChanged(object sender, TextChangedEventArgs e)
        {
            DropShadowEffect glow = new DropShadowEffect();
            glow.Color = Colors.Red;
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim() != "" && _passwordTB1.Text == _passwordTB2.Text)
            {
                glow.Color = Colors.Green;
            }
            _passwordTB1.Effect = _passwordTB2.Effect = glow;
        }

        private void _usernameTb_TextChanged(object sender, TextChangedEventArgs e)
        {
            DropShadowEffect glow = new DropShadowEffect();
            glow.Color = Colors.Red;
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim() != "")
            {
                glow.Color = Colors.Green;
            }
            tb.Effect = glow;
        }

        private void _secureQTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            DropShadowEffect glow = new DropShadowEffect();
            glow.Color = Colors.Red;
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim() != "")
            {
                glow.Color = Colors.Green;
            }
            tb.Effect = glow;
        }

        private void _secureATB_TextChanged(object sender, TextChangedEventArgs e)
        {
            DropShadowEffect glow = new DropShadowEffect();
            glow.Color = Colors.Red;
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim() != "")
            {
                glow.Color = Colors.Green;
            }
            tb.Effect = glow;
        }
        private void FlashTextBox(TextBox tb)
        {
            DoubleAnimation da = new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 0, 0, 200)));
            da.RepeatBehavior = new RepeatBehavior(4);

            tb.BeginAnimation(TextBox.OpacityProperty, da);
        }
    }
}
